﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Ession.OCR
{
    public class PixelTool
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr hWnd);
        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
        [DllImport("gdi32.dll")]
        static extern int GetPixel(IntPtr
         hDC, int
         x, int
         y);
        [DllImport
        ("gdi32.dll"
         )]
        static extern int SetPixel(IntPtr hDC, int
         x, int
         y, int
         color);

        static public Color GetPixel(Control
         control, int
         x, int y)
        {
            Color color = Color.Empty;
            if (control != null
                 )
            {
                IntPtr
         hDC = GetDC(control.Handle);
                int
         colorRef = GetPixel(hDC, x, y);
                color = Color
        .FromArgb(
                    (int
        )(colorRef & 0x000000FF),
                    (int
        )(colorRef & 0x0000FF00) >> 8,
                    (int
        )(colorRef & 0x00FF0000) >> 16);
                ReleaseDC(control.Handle, hDC);
            }
            return color;
        }



        static public Color GetPixel(Image img, int x, int y)
        {
            Color color = Color.Empty;
            if (img != null
                 )
            {
                using (var g = Graphics.FromImage(img))
                {
                    IntPtr p = g.GetHdc();
                    int
             colorRef = GetPixel(p, x, y);
                    color = Color
            .FromArgb(
                        (int
            )(colorRef & 0x000000FF),
                        (int
            )(colorRef & 0x0000FF00) >> 8,
                        (int
            )(colorRef & 0x00FF0000) >> 16);

                    g.ReleaseHdc(p);
                }


            }
            return color;
        }



        static public void SetPixel(Control
         control, int
         x, int
         y, Color
         color)
        {
            if
         (control != null
        )
            {
                IntPtr
         hDC = GetDC(control.Handle);
                int
         argb = color.ToArgb();
                int
         colorRef =
                    (int
        )((argb & 0x00FF0000) >> 16) |
                    (int
        )(argb & 0x0000FF00) |
                    (int
        )((argb & 0x000000FF) << 16);
                SetPixel(hDC, x, y, colorRef);
                ReleaseDC(control.Handle, hDC);
            }
        }





        static public void SetPixel(Image img, int
         x, int
         y, Color
         color)
        {
            if
         (img != null
        )
            {
                using (var g = Graphics.FromImage(img))
                {
                    IntPtr hDC = g.GetHdc();

                    int
                        argb = color.ToArgb();
                    int
                        colorRef =
                            (int
                            )((argb & 0x00FF0000) >> 16) |
                            (int
                            )(argb & 0x0000FF00) |
                            (int
                            )((argb & 0x000000FF) << 16);
                    SetPixel(hDC, x, y, colorRef);
                    g.ReleaseHdc(hDC);
                }
            }
        }
    }
}
